<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\ShopModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class AdvertController extends Controller
{
    /*
    获取广告列表
     user_id:会员id
     page：页号
  */
    public function advert_lists(Request $request){

        $username=$request->input("username")?$request->input("username"):"";
        $page=$request->input("page") && $request->input("page") > 0?$request->param("page"): 1;
        $tel=$request->input("tel")?$request->input("tel"): "";
        $name_firm=$request->input("name_firm")?$request->input("name_firm"): "";
        $location=$request->input("location") ?$request->input("location"): "";

        $where=[];
        $re = DB::table('jy_advert')->select('id','username','tel','name_firm','location','money','pay_status','start_time','end_time','pic');
        if($username<>"") $where[] = ['username','=',$username];
        if($tel<>"") $where[] = ['tel','=',$tel];
        if($name_firm<>"") $where[] = ['name_firm','like',"'%".$name_firm."%'"];
        if($location<>"") $where[] = ['location','=',$location];
        $rs= $re->where($where)->orderBy('id')->paginate(20);
        if($rs){
            $arr=["status"=>1,"msg"=>"获取成功","data"=>$rs];
        }else{
            $arr=["status"=>0,"msg"=>"获取失败","data"=>array()];

        }
        return $arr;
    }
    /*
        获取广告详情
        ad_id:菜品id
    */
    public function advert_details(Request $request){

        $id = $request->param('ad_id');

        if (empty($id)) {
            $arr=["status"=>0,"msg"=>"参数不能为空","data"=>array()];
            return $arr;
        }

        $re = DB::table('jy_ad')->select('id,username,tel,name_firm,location,money,pay_status,start_time,end_time,pic')->where('id','=',$id)->first();
        if($re){
            $arr=["status"=>1,"msg"=>"获取成功","data"=>$re];
        }else{
            $arr=["status"=>0,"msg"=>"获取失败","data"=>array()];

        }
        return $arr;
    }
    /*
        添加/编辑广告
    */
    public function addedit_advert(Request $request){

        $admin_id=session("admin_id");
        if($admin_id==""){
            $arr=["status"=>0,"msg"=>"未登录","data"=>array()];
            return json_encode($arr);
        }
        $data=array();
        $id=$request->param("id")?$request->param("id"):"";
        $username=$request->param("username")?$request->param("username"):"";
        $tel=$request->param("tel")?$request->param("tel"):"";
        $name_firm=$request->param("name_firm")?$request->param("name_firm"):"";
        $start_time=$request->param("start_time")?$request->param("start_time"):"";
        $end_time=$request->param("end_time")?$request->param("end_time"):"";
        $money=$request->param("money")?$request->param("money"):"";
        $pay_status=$request->param("pay_status")?$request->param("pay_status"):"";
        $location=$request->param("location")?$request->param("location"):"";
        // $web_url=$request->param("web_url")?$request->param("web_url"):"";
        $pic=$request->file("pic")?$request->file("pic"):"";
        //参数验证

        if($pic==""){
            $arr=["status"=>0,"msg"=>"请上传广告图片文件。","data"=>array()];
            return json_encode($arr);
        }
        $info=$pic->getInfo();
        if($info["type"]!="image/jpeg"){
            $arr=["status"=>0,"msg"=>"广告图片格式有误。","data"=>array()];
            return json_encode($arr);
        }
        $info = $pic->move(ROOT_PATH . 'public'.DS.'uploads');
        $pic=$this->web_url."/uploads/".$info->getSaveName();
        if($location==""){
            $arr=["status"=>0,"msg"=>"展示位置不能为空。","data"=>array()];
            return json_encode($arr);
        }

        $data["start_time"]=$start_time;
        $data["end_time"]=$end_time;
        $data["tel"]=$tel;
        $data["location"]=$location;
        // $data["web_url"]=$web_url;
        $data["pic"]=$pic;

        $arr=array();
        if($id){
            $res=Db::name("advert")->where("id",$id)->update($data);
            if($res){
                $arr=["status"=>1,"msg"=>"修改成功","data"=>array()];
            }else
                $arr=["status"=>0,"msg"=>"修改失败，请检查数据格式或网络。","data"=>array()];
        }else{
            $data["username"]=$username;
            $data["name_firm"]=$name_firm;
            $data["money"]=$money;
            $data["pay_status"]=$pay_status;

            $res=Db::name("advert")->insertGetId($data);
            if($res){
                $arr=["status"=>1,"msg"=>"添加成功","data"=>array()];
            }else
                $arr=["status"=>0,"msg"=>"添加失败，请检查数据格式或网络。","data"=>array()];

        }
        return json_encode($arr);
    }
    /*
     审核广告
       id:记录id
       status:审核状态 0、不展示 1、展示 缺失
       verify_date 审核时间 缺失
    */
    public function chk_advert(Request $request){

        $admin_id=session("admin_id");
        if($admin_id==""){
            $arr=["status"=>0,"msg"=>"未登录","data"=>array()];
            return json_encode($arr);

        }
        $id=$request->param("id")?$request->param("id"):"";
        $status=$request->param("status") != NULL?$request->param("status"):"";

        $validate = new Validate([
            'id' => 'require',
            'status' => 'require|in:0,1,2',
        ]);
        $data = [
            'id'  => $id,
            'status'  => $status,
        ];
        if (!$validate->check($data)) {
            $arr=["status"=>0,"msg"=>"缺少参数，请检查数据格式。","data"=>array()];
            return json_encode($arr);
        }

        $sql="update jy_ad set admin_id=".$admin_id.",verify_date=".time().",status='".$status."' where id =".$id;
        $re=Db::execute($sql);
        if($re>0){
            $arr=["status"=>1,"msg"=>"审核成功","data"=>array()];
        }else{
            $arr=["status"=>0,"msg"=>"审核失败，请稍后再试","data"=>array()];
        }
        return json_encode($arr);
    }
}

?>